package GoodsBaseService

import (
	"context"
	"gdshop-font-api-go/app/response/GoodsResp"
	toolsDb "gdshop-font-api-go/library/tools/db"
	"strings"
)

// GetGoodsOptionsByGoodsId 获取规格库存，只显示 is_open = 1 的规格
func GetGoodsOptionsByGoodsId(ctx context.Context, goodsId int, limit int) []*GoodsResp.GoodsOption {
	goodsOptions := []*GoodsResp.GoodsOption{}
	goDb := toolsDb.GetUnSafaTableAddDeleteWhere(
		ctx,
		"goods_option").Where("goods_id=? AND is_open=1",
		goodsId,
	).Order("display_sort DESC")
	if limit > 0 {
		goDb = goDb.Limit(limit)
	}
	err := goDb.Structs(&goodsOptions)
	if err != nil {
		return nil
	}
	for _, item := range goodsOptions {
		item.TitleArr = strings.Split(item.Title, ";")
		item.Title = strings.ReplaceAll(item.Title, ";", ",")
	}
	return goodsOptions
}
